package com.zeyi.chat.controller;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.UserManager;
import com.zeyi.chat.po.ChatGroup;
import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.SocketUtil;
import com.zeyi.chat.views.Alert;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

/**
 * @author zeyi
 */
public class EditUserInfoController {
    private static EditUserInfoController instance;

    @FXML
    private TextArea signature;

    @FXML
    private TextArea address;

    @FXML
    private TextField name;

    public static EditUserInfoController getInstance() {
        return instance;
    }

    @FXML
    private void initialize() {
        instance = this;
    }

    @FXML
    public void onClose(MouseEvent mouseEvent) {
        Stage stage = (Stage) name.getScene().getWindow();
        stage.close();
    }

    public void initData() {
        User currentUser = UserManager.getCurrentUser();
        name.setText(currentUser.getUsername());
        signature.setText(currentUser.getPersonalSignature());
        address.setText(currentUser.getAddress());
    }

    public void saveInfo(MouseEvent mouseEvent) {
        Stage stage = (Stage) name.getScene().getWindow();
        if (name.getText().equals("")) {
            new Alert("昵称不能为空", stage);
            return;
        }
        User currentUser = UserManager.getCurrentUser();
        currentUser.setUsername(name.getText());
        currentUser.setPersonalSignature(signature.getText());
        currentUser.setAddress(address.getText());
        SocketUtil.send(new Message("ChatHandler/updateUserInfo", JSON.toJSONString(currentUser)));
        ChatInfoController.getInstance().displayUserInfo(currentUser, true);
        stage.close();
    }
}
